/**
 * Genarator by KKDGen v<?php echo $version."\n";?>
 * @author KKDeveloeper
 * @email Dixonsatit@gmail.com
 * @link www.ikkdev.com,www.kkdeveloper.com
 <?php  echo empty($nameSpace)?'':'* Namespace : '.$nameSpace."\n"; ?>
 * ExtjsController : <?php echo $modelClass."\n";?>
 */

Ext.define('<?php echo empty ($nameSpace)? $modelClass:$nameSpace.'.controller.'.$modelClass; ?>', {
    extend: 'Ext.app.Controller',
    stores: ['<?php echo $modelClass; ?>'],
    models: ['<?php echo $modelClass; ?>'],
   statics: {
        rowEdit:false
    },
    refs:[{ref:'Grid',selector:'<?php echo strtolower($modelClass); ?>Grid'},
            {ref:'Form',selector:'<?php echo strtolower($modelClass);  ?>Form'},
            {ref:'Main',selector:'<?php echo strtolower($modelClass);  ?>Main'}],
    init: function() {
        
        console.log('Controler <?php echo $modelClass?> Ready...');
        
        this.control({
            '<?php echo strtolower($modelClass);  ?>Grid button[action=add]': {
                click: this.acAdd
            },
            '<?php echo strtolower($modelClass);  ?>Form button[action=back]': {
                click: this.acBack
            }, 
            '<?php echo strtolower($modelClass);  ?>Form button[action=add]': {
                click: this.acAdd
            }, 
            '<?php echo strtolower($modelClass);  ?>Form': {
                //fireEvent on form
                create:this.onSave
            }, 
            '<?php echo strtolower($modelClass);  ?>Form button[action=delete]': {
                click: this.onDeleteClick
            }, 
            '<?php echo strtolower($modelClass);  ?>Grid': {
                itemdblclick: this.gridDbClick,
                selectionchange: function(selModel, selected) 
                {
                    this.getForm().setActiveRecord(selected[0] || null);
                }
            }
        });
    }, 
    onLaunch: function() 
    {
        
    },
    gridDbClick :  function(model, records)
    {
        if(<?php echo empty ($nameSpace)? $modelClass:$nameSpace.'.controller.'.$modelClass; ?>.rowEdit===false)
        {  
            this.getForm().setStatusbar('Update : '+records.get('id'));
            this.getGrid().plugins[0].cancelEdit();
            
            this.getForm().loadRecord(records);
            this.getMain().getLayout().setActiveItem(1);
            //this.getForm().down('#save').setDisabled(false);
            this.getForm().down('#deleteInform').setDisabled(false);
        }
    },
    acAdd :function(btn)
    {
         if(<?php echo empty ($nameSpace)? $modelClass:$nameSpace.'.controller.'.$modelClass; ?>.rowEdit===true)
        {
            var grid = this.getGrid();
            this.getRestStore().insert(0, this.getRestModel().create());
            grid.plugins[0].startEdit(0, 1);
        }
        else
        {
            this.getForm().setStatusbar('Create new record.');
            
            this.getForm().onReset();
            this.getMain().getLayout().setActiveItem(1);
            //this.getForm().down('#save').setDisabled(true);
            this.getForm().down('#deleteInform').setDisabled(true);
        }
    },
    acBack :function(btn)
    {
        this.getMain().getLayout().setActiveItem(0);
    },
    onSave :  function(form,data)
    {
        this.get<?php echo $modelClass; ?>Store().insert(0, data);
        this.getMain().getLayout().setActiveItem(0);
    },
    onDeleteClick: function(btn)
    {
        Ext.Msg.show({
            title:'Confirm Delete',
            msg: 'คุณต้องการลบข้อมุลใช่หรือไม่..',
            buttons: Ext.Msg.YESNO,
            fn: this.onDelete,
            icon: Ext.MessageBox.WARNING,
            scope: this
        });
    },
    onDelete:function(btn)
    {
        if(btn=='yes')
        {
            if(this.getGrid().onDelete(btn))
            {
                this.getForm().getForm().reset();
                this.getMain().getLayout().setActiveItem(0);
            }
        }
    }
});

